package ControllerStarter;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import Controllers.BoardController;
import Controllers.BoardControllerInterface;
import TechStuff.RmiStarter;


public class BoardControllerStarter extends RmiStarter {

	public BoardControllerStarter()  {
		super(BoardControllerInterface.class);
		doCustomRmiHandling();
	}

	@Override
	public void doCustomRmiHandling() {
		try {
			BoardControllerInterface obj = new BoardController();
			BoardControllerInterface stub = (BoardControllerInterface) UnicastRemoteObject.exportObject(obj, 0);

		    // Bind the remote object's stub in the registry
		    Registry registry = LocateRegistry.getRegistry();
		    registry.bind(BoardControllerInterface.name, stub);

		    System.err.println("BoardEngineStarter ready");
		} catch (Exception e) {
		    System.err.println("BoardEngineStarter exception: " + e.toString());
		    e.printStackTrace();
		}
		
	}
	
	public String createBoard(int index)
	{
		try {
			BoardControllerInterface obj = new BoardController();
			BoardControllerInterface stub = (BoardControllerInterface) UnicastRemoteObject.exportObject(obj, 0);

		    // Bind the remote object's stub in the registry
		    Registry registry = LocateRegistry.getRegistry();
		    String name = BoardControllerInterface.name + index;
		    registry.bind(name, stub);

		    System.err.println(name + " ready");
		    return name;
		} catch (Exception e) {
		    System.err.println("BoardEngineStarter exception: " + e.toString());
		    e.printStackTrace();
		    return null;
		}
	}

}
